// pages/personal/personal.js
var api = require('../../utils/api.js');
var app = getApp();
var regeneratorRuntime = require("../../utils/regenerator-runtime/runtime");
var token = wx.getStorageSync('token')
Page({
  // 设置分享到朋友圈的标题
  onShareTimeline: function () {
    return {
      title: "智克艺术 - 个人中心",
      query: ""
    }
  },
  /**
   * 页面的初始数据
   */
  data: {
    openid: '',
    isHidden: false,
    text: '', //滚动的文字
    type: '',
    schIdx: 0,
    schoolArr: [],
    inforArr: [{
        icon: 'http://qnwmfx.laigl.com/iconimg/icon_5.png',
        title: '报班信息'
      },
      {
        icon: 'http://qnwmfx.laigl.com/iconimg/icon_6.png',
        title: '考勤信息'
      },
      {
        icon: 'http://qnwmfx.laigl.com/iconimg/icon_7.png',
        title: '比赛考级'
      },
      {
        icon: 'http://qnwmfx.laigl.com/iconimg/icon_29.png',
        title: '课程介绍'
      },
      {
        icon: 'http://qnwmfx.laigl.com/iconimg/icon_25.png',
        title: '我的歌单'
      },

      {
        icon: 'http://qnwmfx.laigl.com/iconimg/shop_car.png',
        title: '购物车'
      },
      {
        icon: 'http://qnwmfx.laigl.com/iconimg/order.png',
        title: '我的订单'
      },
      {
        icon: 'http://qnwmfx.laigl.com/iconimg/icon_27.png',
        title: '我的视频'
      },
      {
        icon: 'http://qnwmfx.laigl.com/icon/zk_my_photo.png',
        title: '我的图片'
      },
      {
        icon: 'http://qnwmfx.laigl.com/iconimg/address_icon.png',
        title: '地址管理'
      },
      {
        icon: 'http://qnwmfx.laigl.com/iconimg/icon_8.png',
        title: '修改密码'
      },
      {
        icon: 'http://qnwmfx.laigl.com/iconimg/icon_9.png',
        title: '意见反馈'
      },
      {
        icon: 'http://qnwmfx.laigl.com/iconimg/logout.png',
        title: '注销登录'
      },
    ], //家长中心
    inforArr1: [{
        icon: 'http://qnwmfx.laigl.com/iconimg/icon_20.png',
        title: '点名'
      },
      {
        icon: 'http://qnwmfx.laigl.com/iconimg/icon_6.png',
        title: '考勤信息'
      },
      {
        icon: 'http://qnwmfx.laigl.com/iconimg/icon_25.png',
        title: '我的歌单'
      },
      {
        icon: 'http://qnwmfx.laigl.com/icon/zk_my_photo.png',
        title: '我的图片'
      },
      {
        icon: 'http://qnwmfx.laigl.com/iconimg/shop_car.png',
        title: '购物车'
      },
      {
        icon: 'http://qnwmfx.laigl.com/iconimg/order.png',
        title: '我的订单'
      },
      {
        icon: 'http://qnwmfx.laigl.com/iconimg/address_icon.png',
        title: '地址管理'
      },
      {
        icon: 'http://qnwmfx.laigl.com/iconimg/icon_8.png',
        title: '修改密码'
      },
      {
        icon: 'http://qnwmfx.laigl.com/iconimg/logout.png',
        title: '注销登录'
      },
    ], //教师中心
    inforArr2: [

      {
        icon: 'http://qnwmfx.laigl.com/iconimg/icon_6.png',
        title: '考勤确认'
      },
      {
        icon: 'http://qnwmfx.laigl.com/iconimg/icon_21.png',
        title: '学生管理'
      },
      {
        icon: 'http://qnwmfx.laigl.com/icon/zk_my_photo.png',
        title: '我的图片'
      },
      {
        icon: 'http://qnwmfx.laigl.com/iconimg/shop_car.png',
        title: '购物车'
      },
      {
        icon: 'http://qnwmfx.laigl.com/iconimg/order.png',
        title: '我的订单'
      },
      {
        icon: 'http://qnwmfx.laigl.com/iconimg/address_icon.png',
        title: '地址管理'
      },
      {
        icon: 'http://qnwmfx.laigl.com/iconimg/logout.png',
        title: '注销登录'
      },

    ], //前台中心
    inforArr3: [{
        icon: 'http://qnwmfx.laigl.com/iconimg/icon_21.png',
        title: '学生统计'
      },
      {
        icon: 'http://qnwmfx.laigl.com/iconimg/icon_22.png',
        title: '财务报表'
      },
      {
        icon: 'http://qnwmfx.laigl.com/iconimg/icon_6.png',
        title: '教师考勤'
      },
      {
        icon: 'http://qnwmfx.laigl.com/iconimg/order.png',
        title: '我的订单'
      },
      {
        icon: 'http://qnwmfx.laigl.com/iconimg/shop_car.png',
        title: '购物车'
      },
      {
        icon: 'http://qnwmfx.laigl.com/iconimg/address_icon.png',
        title: '地址管理'
      },
      {
        icon: 'http://qnwmfx.laigl.com/iconimg/fx.png',
        title: '我的分销'
      },

      {
        icon: 'http://qnwmfx.laigl.com/iconimg/icon_25.png',
        title: '我的歌单'
      },
      {
        icon: 'http://qnwmfx.laigl.com/iconimg/icon_27.png',
        title: '我的视频'
      },
      {
        icon: 'http://qnwmfx.laigl.com/icon/zk_my_photo.png',
        title: '我的图片'
      },
      {
        icon: 'http://qnwmfx.laigl.com/iconimg/icon_8.png',
        title: '修改密码'
      },
      {
        icon: 'http://qnwmfx.laigl.com/iconimg/logout.png',
        title: '注销登录'
      },
    ], //校长中心
    inforArr4: [{
        icon: 'http://qnwmfx.laigl.com/iconimg/order.png',
        title: '我的订单'
      },
      {
        icon: 'http://qnwmfx.laigl.com/iconimg/shop_car.png',
        title: '购物车'
      },
      {
        icon: 'http://qnwmfx.laigl.com/iconimg/address_icon.png',
        title: '地址管理'
      },
      {
        icon: 'http://qnwmfx.laigl.com/iconimg/fx.png',
        title: '我的分销'
      },
      {
        icon: 'http://qnwmfx.laigl.com/iconimg/icon_25.png',
        title: '我的歌单'
      },
      {
        icon: 'http://qnwmfx.laigl.com/iconimg/icon_27.png',
        title: '我的视频'
      },
      {
        icon: 'http://qnwmfx.laigl.com/icon/zk_my_photo.png',
        title: '我的图片'
      },
      {
        icon: 'http://qnwmfx.laigl.com/iconimg/icon_8.png',
        title: '修改密码'
      },
      {
        icon: 'http://qnwmfx.laigl.com/iconimg/logout.png',
        title: '注销登录'
      },
    ], //分校长中心
    accountParams: {},
    isTourist: false, //登录框显示隐藏
    touristShow: false, //游客板块显示隐藏
    btnShow: true,
    youBtn: true,
    canIUse:wx.canIUse('open-data')
  },

  // 机构会员账号登录
  touristLogin() {
    this.setData({
      isTourist: true,
      btnShow: false,
      touristShow: false
    })
  },

  //游客登录
  userLogin() {
    this.setData({
      touristShow: true,
      youBtn: true
    })
  },

  //切换游客登录
  checkTourist() {
    this.setData({
      //btnShow修改为false
      btnShow: true,
      isTourist: false,
      youBtn: true,
      touristShow:true
    })
  },

  //游客绑定手机号码
  bindPhone() {
    wx.navigateTo({
      url: "/pages/bindPhone/bindPhone",
    })
  },
  //跳转购物车
  toShopCar() {
    wx.navigateTo({
      url: "/pages/shopCar/shopCar",
    })
  },

  //跳转我的订单
  myOrder() {
    wx.navigateTo({
      url: "/pages/myOrder/myOrder",
    })
  },

  //跳转地址管理页面
  toAddress() {
    wx.navigateTo({
      url: "/pages/address/address",
    })
  },

  //游客歌单
  myMusicList() {
    wx.navigateTo({
      url: "/pages/myMusic/myMusic",
    })
  },
  sendMsg(e) {

    let idx = e.currentTarget.dataset.idx;
    if (idx == 10) {
      return
    }
    wx.requestSubscribeMessage({
      tmplIds: app.globalData.tmplIds,
      success(res) {
        console.log(res)
      },
      fail: err => {
        console.log(err)
      }
    })

  },
  //登录
  handleLogin(e) {
    var form_id = e.detail.formId
    this.setData({
      accountParams: e.detail.value
    })
    if (!this.data.accountParams.account) {
      wx.showToast({
        title: '请输入账号',
        icon: 'none'
      })
      return
    }

    if (!this.data.accountParams.pwd) {
      wx.showToast({
        title: '请输入密码',
        icon: 'none'
      })
      return
    }
    var openid = wx.getStorageSync('openid')
    api.fetchApi('login/login', {
      ...this.data.accountParams,
      openid
    }).then(res => {
      var type = res.type,
        token = res.token;
      wx.showToast({
        title: '登录成功',
        icon: 'none'
      })
      wx.setStorageSync('type', res.type);
      wx.setStorageSync('token', res.token);
      this.getShopCar();
      this.getOrderList();
      //收集formid
      // api.fetchApi('Formid/save_user_formid', { form_id, token: wx.getStorageSync('token') })

      //记住账号密码功能
      wx.setStorageSync('account', this.data.accountParams)
      this.setData({
        type: type,
        outline: false,
        isTourist: false,
        touristShow:false, 
        btnShow: false,
        youBtn: false
        // openid
      })

      this.getTeacherName();
      if (this.data.type == 7) {
        this.getCaump();
      }
    })
  },

  getTeacherName() {
    //获取教师名字
    if (this.data.type == 8) {
      api.fetchApi('teacher/getinfo', {
        token: wx.getStorageSync('token')
      }).then(res => {
        var teacherName = res.teacher_name;
        this.setData({
          teacherName
        })
      })
    }
  },

  //跳转会员注册
  toAssigned() {
    wx.navigateTo({
      url: "/pages/assigned/assigned"
    })
  },

  // 跳转忘记密码
  toFindPwd() {
    wx.navigateTo({
      url: '/pages/findPwd/findPwd',
    })
  },

  //切换学校
  schoolPickerChange(e) {
    // var nowId = e.currentTarget.id;
    var nowArr = this.data.schoolArr[this.data.schIdx];
    var schoolId = nowArr.id;
    this.setData({
      schIdx: e.detail.value,
      schoolId
    })

  },

  //注销登录
  logout() {
    wx.removeStorageSync('type');
    wx.removeStorageSync('token');
    wx.getStorageSync('isDian') == 1;
    api.showToast('注销成功').then(() => {
      api.fetchApi('Wechat/add_tourist', {
        openid: app.globalData.openid
      }).then(res => {
        let token = res.token;
        wx.setStorageSync('token', token)
      })
      setTimeout(() => {
        wx.reLaunch({
          url: '../personal/personal',
        })
        this.setData({
          type: ''
        })
      }, 1500)
    })
  },
  // 游客个人中心跳转详情
  toDetai0(e){
    const title = e.currentTarget.dataset.title;
    switch(title){
      case "我的歌单":
        wx.navigateTo({
          url: "../myMusic/myMusic",
        });
        break;
      case "我的视频":
        wx.navigateTo({
          url: "../myVideo/myVideo?upVideo=" + this.data.upVideo,
        });
        break;
      case "我的图片":
        wx.navigateTo({
          url: "/test/pages/searchList/searchList?isLink=true",
        });
        break;
    }
  },
  //家长个人中心跳转详情
  toDetai(e) {
    var id = e.currentTarget.id
    switch (id) {
      case "4":
        wx.navigateTo({
          url: "../myMusic/myMusic",
        })
        break
      case "0":
        //课程介绍
        wx.navigateTo({
          url: '../parentClassInfor/parentClassInfor',
        })
        break;
      case "1":
        wx.navigateTo({
          url: '../parentKaoqinInfor/parentKaoqinInfor',
        })
        break
      case "2":
        wx.navigateTo({
          url: '../parentLevel/parentLevel',
        })
        break
      case "3":
        wx.navigateTo({
          url: '../course/course?schoolId=' + this.data.schoolId,
        })
        break
      case "5":
        wx.navigateTo({
          url: '/pages/shopCar/shopCar',
        })
        break
      case "6":
        wx.navigateTo({
          url: '/pages/myOrder/myOrder',
        })
        break
      case "7":
        wx.navigateTo({
          url: '/pages/myVideo/myVideo?upVideo=' + this.data.upVideo,
        }) 
      case "8":
        wx.navigateTo({
          url: '/pages/address/address',
        })
        break
      case "9":
        wx.navigateTo({
          url: '../parentChangePsw/parentChangePsw',
        })
        break
      case "10":
        wx.navigateTo({
          url: '../parentChangePsw/parentChangePsw',
        })
        break
      case "11":
        this.logout()
        break
    }
  },

  //教师个人中心跳转详情
  toDetai1(e) {
    var id = e.currentTarget.id;
    switch (id) {
      case "2":
        wx.navigateTo({
          url: "../myMusic/myMusic",
        })
        break
      case "0":
        wx.navigateTo({
          url: '../teacherName/teacherName',
        })
        break
      case "1":
        wx.navigateTo({
          url: '../teacherKaoqinInfor/teacherKaoqinInfor',
        })
        break
      case "3":
        wx.navigateTo({
          url: '/pages/shopCar/shopCar',

        })
        break
      case "4":
        wx.navigateTo({
          url: '/pages/myOrder/myOrder',

        })
        break
      case "5":
        wx.navigateTo({
          url: '/pages/address/address',
        })
        break
      case "6":
        wx.navigateTo({
          url: '/test/pages/searchList/searchList?token=' + token + '&isLink=' + true,
        })
        break
      case "7":
        wx.navigateTo({
          url: '../parentChangePsw/parentChangePsw',
        })
        break
      case "8":
        this.logout()
        break

    }
  },

  //前台个人中心跳转详情
  toDetai2(e) {
    var id = e.currentTarget.id;
    switch (id) {
      case "0":
        wx.navigateTo({
          url: '../frontKaoqin/frontKaoqin',

        })
        break
      case "1":
        wx.navigateTo({
          url: '../frontSdt/frontSdt',

        })
        break
      case "2":
        wx.navigateTo({
          url: '/pages/shopCar/shopCar',

        })
        break
      case "3":
        wx.navigateTo({
          url: '/pages/myOrder/myOrder',

        })
        break
      case "4":
        wx.navigateTo({
          url: '/pages/address/address',

        })
        break

      case "5":
        this.logout()
        break
    }
  },

  //校长个人中心跳转详情
  toDetai3(e) {
    var id = e.currentTarget.id;
    switch (id) {
      case "0":
        wx.navigateTo({
          url: '../studentTotal/studentTotal',
        })
        break
      case "1":
        wx.navigateTo({
          url: '../caiwuTable/caiwuTable',
        })
        break
      case "2":
        wx.navigateTo({
          url: '../teachersWork/teachersWork',
        })
        break
      case "3":
        wx.navigateTo({
          url: '/pages/myOrder/myOrder',
        })
        break
      case "4":
        wx.navigateTo({
          url: '/pages/shopCar/shopCar',
        })
        break
      case "5":
        wx.navigateTo({
          url: '/pages/address/address',
        })
        break
      case "6":
        wx.navigateTo({
          url: '/pages/distribution/distribution',
        })
        break
      case "7":
        wx.navigateTo({
          url: "../myMusic/myMusic",
        })
        break
      case "8":
        wx.navigateTo({
          url: "../myVideo/myVideo?upVideo=" + this.data.upVideo,
        })
        break
      case "9":
        wx.navigateTo({
          url: '/test/pages/searchList/searchList?isLink=' + true,
        })
        break
      case "10":
        wx.navigateTo({
          url: '../parentChangePsw/parentChangePsw',
        })
        break
      case "11":
        this.logout()
        break
    }
  },

  //副校长个人中心跳转详情
  toDetai4(e) {
    var id = e.currentTarget.id;

    switch (id) {
      case "0":
        wx.navigateTo({
          url: '/pages/myOrder/myOrder',
        })
        break
      case "1":
        wx.navigateTo({
          url: '/pages/shopCar/shopCar',
        })
        break
      case "2":
        wx.navigateTo({
          url: '/pages/address/address',
        })
        break
      case "3":
        wx.navigateTo({
          url: '/pages/distribution/distribution',
        })
        break
      case "4":
        wx.navigateTo({
          url: "../myMusic/myMusic",
        })
        break
      case "5":
        wx.navigateTo({
          url: "../myVideo/myVideo?upVideo=" + this.data.upVideo,
        })
        break
      case "6":
        wx.navigateTo({
          url: '/test/pages/searchList/searchList?isLink=' + true,
        })
        break
      case "7":
        wx.navigateTo({
          url: '../parentChangePsw/parentChangePsw',
        })
        break
      case "8":
        this.logout()
        break
    }
  },

  //授权回调
  toAuthorize(e) {
    wx.removeStorageSync('type');
    wx.removeStorageSync('token');
    app.globalData.err = ''
    this.setData({
      isHidden: false,
      isQuan: true
    })
    wx.reLaunch({
      url: '../personal/personal'
    })
  },

  cancel() {
    this.setData({
      isHidden: false
    })
  },

  //获取提示信息
  getNotice() {
    api.fetchApi('student/student_notice', {
      token: wx.getStorageSync('token')
    }).then(res => {
      this.setData({
        text: res
      })
    })
  },

  //获取所在学校
  getCaump() {
    api.fetchApi('student/get_student_school', {
      token: wx.getStorageSync('token')
    }).then(res => {
      this.setData({
        schoolArr: res
      })
      //默认学校id获取
      var nowArr = this.data.schoolArr[this.data.schIdx];

      var schoolId = nowArr.id;
      this.setData({
        schoolId
      })

    })
  },

  //查询购物车加红点
  getShopCar() {
    api.fetchApi('Cart/index', {
        token: wx.getStorageSync('token')
      })
      .then(res => {
        let goodsList = res.rows;
        let is_no = res.is_no
        let send_video = res.send_video
        let username = res.username
        this.setData({
          username,
          is_no,
          goodsList,
          upVideo: send_video
        })
      })
  },

  //查询订单列表加红点
  getOrderList() {
    let params = {
      token: wx.getStorageSync('token'),
      status: 0,
    }
    api.fetchApi('/order/my_order', params).then(res => {
      this.setData({
        no_pay: res.no_pay,
        no_receipt: res.no_receipt,
        no_ship: res.no_ship
      })
    })
  },
  // 检查是否已经被挤掉
  checkSign() {
    let token = wx.getStorageSync('token')
    const that = this
    wx.request({
      url: app.globalData.host + 'Test/login_status',
      data: {
        token
      },
      method: 'POST',
      success(res) {
        if (res.data.code == 200) {} else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          })
          that.setData({
            btnShow: true,
            youBtn: true,
            type: ''
          })
          wx.clearStorage()
        }
      },
      fail(err) {

      }
    })
  },
  getUserInfo(){
    // console.log("getUserInfo");
        wx.getUserProfile({
          desc:"获取用户基本信息",
      success:res=>{
       var userInfo = res.userInfo
        wx.setStorageSync('userInfo', userInfo)
        this.setData({
          userInfo
        })
      },
      fail:err=>{
        console.log(err);
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.globalData.isProfile = true
    var type = wx.getStorageSync('type');
    var token = wx.getStorageSync('token')
    app.globalData.token = token;
    let touristShow = true;
    // console.log(this.data.isTourist);
    if(type!=""){
       touristShow = false 
    }
    this.setData({
      type,
      touristShow
    })
    // console.log(wx.canIUse('getUserProfile'));

    wx.getUserInfo({
      success: res => {
        // console.log(res);
        var userInfo = res.userInfo
        wx.setStorageSync('userInfo', userInfo)
        this.setData({
          userInfo
        })
      }
    })
    if(this.data.type==""){
      this.userLogin()
    }
    //api.fetchApi('student/student_notice', { token: wx.getStorageSync('token')})
    this.getTeacherName();
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    //  this.test()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.globalData.isProfile = true
    this.checkSign()
    this.getShopCar();
    this.getOrderList();
    //记住账号密码功能
    var account = wx.getStorageSync("account")
    //console.log(account)
    this.setData({
      account
    })


    if (wx.getStorageSync('type')) {
      this.setData({
        btnShow: false,
        youBtn: false,
        // isTourist: true,
        // touristShow:false
      })
    }

    //掉线处理
    // console.log(app.globalData.err)
    if (app.globalData.err) {
      // this.setData({
      //   isHidden: true,
      // })
      // api.showToast('您已掉线!','http://qnwmfx.laigl.com/iconimg/warn.png').then(()=>{
      setTimeout(() => {
        wx.removeStorageSync('type');
        wx.removeStorageSync('token');
        wx.reLaunch({
          url: '../personal/personal'
        })
        app.globalData.err = ''
      }, 1000)
      // })


    }

    // console.log(wx.getStorageSync('token'))
    if (wx.getStorageSync('token')) {
      this.getNotice();

      if (this.data.type == 7) {
        this.getCaump();
      }

    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    app.globalData.isProfile = false
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})